<?php

/**
 * alltosun.com  index.php
 * ============================================================================
 * 版权所有 (C) 2009-2013 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 王德康 (wangdk@alltosun.com) $
 * $Date: 2016-4-29 下午12:52:30 $
 * $Id$
 */
class Action
{
    private $per_page = 20;

    /**
     * 显示渠道列表
     */
    public function index() {

        $page = Request::Get('page_no', 1);
        $status = Request::Get('status', 1);
        $search_filter = Request::getParam('search_filter', array());

        $filter = array();
        $filter['status'] = $status;
        $store_category = store_config::$type_arr;

        $channel_list = get_data_list('channel', $filter);
        if ($channel_list) {
            foreach ($channel_list as $k => $v) {
                $channel_list[$k]['type_title'] = $store_category[$v['type']];
            }
        }

        Response::assign('channel_list', $channel_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/list.html');
    }

    /**
     * 添加渠道
     */
    public function add() {
        $store_category = store_config::$type_arr;
        Response::assign('store_category', $store_category);
        Response::display('admin/add.html');
    }

    /**
     * 编辑渠道
     */
    public function edit() {
        $id = Request::getParam('id', 0);

        $channel_info = _uri('channel', $id);

        Response::assign('channel_info', $channel_info);
        Response::assign('id', $id);

        $store_category = store_config::$type_arr;
        Response::assign('store_category', $store_category);
        Response::display('admin/add.html');
    }

    /**
     * 保存
     */
    public function save() {
        $info = Request::getParam('info', array());
        $id = Request::getParam('id', 0);

        if (!isset($info['title']) || !$info['title']) {
            return '请填写渠道标题';
        }

        if (!isset($info['channel_desc']) || !$info['channel_desc']) {
            return '请填写渠道描述';
        }


        if (!isset($info['channel_desc']) || !$info['channel_desc']) {
            return '请填写渠道描述';
        }

        if ($id) {
            _model('channel')->update($id, $info);
        } else {
            _model('channel')->create($info);
        }

        Response::redirect(AnUrl('channel/admin'));
    }

    /**
     * 删除
     * @return array
     */
    public function delete() {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k => $v) {
            if (!$v) {
                continue;
            }

            $category_info = _uri('channel', $v);
            if (1 == $category_info['status']) {
                _model('channel')->update($v, array('status' => 0));
            }
        }

        return 'ok';
    }

    /**
     * 更新状态
     * @return array
     */
    public function change_status() {
        $id = Request::getParam('id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $channel_info = _uri('channel', $id);
        if ($status == $channel_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('channel')->update($id, array('status' => 1));
        }

        return 'ok';
    }
}

?>